<template>
    <div></div>
</template>
<script>
import {param2Obj} from "@/util";
import {getToken} from "@/util/auth";

export default {
    name: "loading",
    data() {
        return {
            loading: "",
        }
    },
    mounted() {
        this.login()
    },
    methods: {
        // 没有token并且当前地址栏有code 获取code调用登录接口
        login() {
            if (!getToken() && window.location.href.includes('code=')) {
                this.loading = this.$loading({
                    lock: true,
                    text: '单点系统登录中,请稍后。。。',
                    spinner: 'el-icon-loading',
                })

                let obj = param2Obj(window.location.href);

                this.$store.dispatch('LoginBySso', {code: obj.code, state: 1, source: 1, tenantId: 1}).then(() => {
                    let key_1 = setTimeout(() => {
                        document.dispatchEvent(new Event("haveToken"));
                        this.$store.dispatch("GetMenu").then(data => {
                            if (data.length !== 0) {
                                this.$router.$avueRouter.formatRoutes(data, true);

                                let key_2 = setTimeout(() => {
                                    this.$router.push(obj.path ? obj.path : "/wel/index");
                                    this.loading.close();

                                    clearInterval(key_1)
                                    clearInterval(key_2)
                                }, 500)
                            }
                        })
                    }, 500)
                }).catch(() => {
                    this.loading.close();
                });
            } else {
                this.$router.push("/wel/index");
            }
        }
    },
}
</script>
